#coding=utf-8
from django.db import models
import types

class StrListField(models.TextField):
    description = "save a list of strings into the database"
    __metaclass__ = models.SubfieldBase #to_python won't be call unless this line exists
    
    def to_python(self, value):
        #print '<-db', type(value), value
        if(type(value) == (types.UnicodeType or types.StringType)):
            return value.split('*')
        elif(isinstance(value, list)):
            return value
        else:
            return []

    def get_db_prep_value(self, value):
        #print '->db1', value
        return '*'.join(value)
    
    def db_type(self):
        return 'longtext'

class IntegerListField(models.TextField):
    description = "save a list of strings into the database"
    __metaclass__ = models.SubfieldBase #to_python won't be call unless this line exists
    
    def to_python(self, value):
        #print '<-db', type(value), value
        if(type(value) == (types.UnicodeType or types.StringType)):
            if len(value):            
                return [int(s) for s in value.split('*')]
            return []
        elif(isinstance(value, list)):
            return value
        else:
            return []

    def get_db_prep_value(self, value):
        #print '->db1', value
        return '*'.join([str(i) for i in value])
    
    def db_type(self):
        return 'longtext'


class DoubanSubject(models.Model):
    title = models.CharField(max_length=128)
    url = models.CharField(max_length=128)
    rating = models.FloatField(default = 0.0)
    rating_count = models.IntegerField(default = 0)
    tags = StrListField()
    tag_counts = IntegerListField()
    
class Photos(models.Model):
    uid = models.CharField(max_length=32)#id of the photo owner
    path = models.CharField(max_length=128)
    photo = models.ImageField(upload_to = 'citylife/%Y/%m/%d')

class User(models.Model):
    selection_regions = (('beijing', 'beijing'), ('shanghai', 'beijing'), ('xianggang', 'xianggang'), ('yunnan', 'yunnan'), )
    selection_industries = (('gongye', 'gongye'), ('nongye', 'nongye'), ('fuwuye', 'fuwuye'))

    name = models.CharField(max_length = 100)
    psd = models.CharField(max_length=128)
    douban_id = models.CharField(max_length=128, blank = True)
    region = models.CharField(max_length = 100, choices = selection_regions, blank = True)
    industry = models.CharField(max_length = 100, choices = selection_industries, blank = True)
    phone_personal = models.IntegerField(blank = True, default = 0)
    phone_work = models.IntegerField(blank = True, default = 0)
    address = models.CharField(max_length = 1000, blank = True)
    MSN = models.EmailField(blank = True)
    QQ = models.IntegerField(blank = True, default = 0)
    email = models.EmailField()
    website = models.URLField(blank = True)
    interests = models.TextField(blank = True)
    honors = models.TextField(blank = True)
    more = models.TextField(blank = True)

    working_position_ids = IntegerListField()
    education_record_ids = IntegerListField()
    
    douban_access_key = models.CharField(max_length=128)    
    douban_access_secret = models.CharField(max_length=128)

    douban_subject_ids = IntegerListField()
    top_tags = StrListField() #top tags of this user
    top_tag_counts = IntegerListField() #top_tag_counts[tag0] = number of times tag0 is used by this user
    tagCoord = IntegerListField() # tag coordination
    rec_friend_ids = IntegerListField() # IDs of the recommended friends
    rec_subject_ids = IntegerListField() # IDs of the recommended friends
    rec_group_ids = IntegerListField() # IDs of the recommended groups
    updated = models.IntegerField(default = 0)

    def __str__(self):
        return self.name

class WorkingPosition(models.Model):
    company_name = models.CharField(max_length = 100)
    title = models.CharField(max_length = 100)
    time_from = models.DateField(help_text="Please use the following format: <em>YYYY-MM-DD</em>")
    time_to = models.DateField(help_text="Please use the following format: <em>YYYY-MM-DD</em>")
    desc = models.TextField()

class EducationRecord(models.Model):
    school_name = models.CharField(max_length = 100)
    degree = models.CharField(max_length = 100)
    major = models.CharField(max_length = 100)
    time_from = models.CharField(max_length = 100, help_text="Please use the following format: <em>YYYY-MM-DD</em>")
    time_to = models.CharField(max_length = 100, help_text="Please use the following format: <em>YYYY-MM-DD</em>")

class Job(models.Model):
    owner = models.ForeignKey(User)
    title = models.CharField(max_length=128)
    category = models.CharField(max_length=128)
    desc = models.CharField(max_length=4096)
    bbids = StrListField() #IDs of bids to this job

class Bid(models.Model):
    uid = models.CharField(max_length=32)#id of the bidder
    jid = models.CharField(max_length=32)#id of the job bidded
    price = models.IntegerField()
    desc = models.CharField(max_length = 4096)
